class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<int>f(n + 1);
        f[n] = 0;
        for (int i = n - 1; i >= 0; i--) {
            f[i] = max(f[i + 1], prices[i]);
        }
        int ma = 0;
        for (int i = 0; i < n - 1; i++) {
            ma = max(ma, f[i] - prices[i]);
        }
        return ma;
    }
};